/**
 * StreetsDateTime By Jimmy
 * based on wicket-data.js
 */
if (typeof(Streets) == "undefined")
	Streets = { };
	
Streets.DateTime = { }


Streets.DateTime.parseDate = function(pattern, value) {
	numbers = value.match(/(\d+)/g);
	if (numbers == null) return Number.NaN;
	var day, month, year;
	arrayPos = 0;
	for (i = 0; i < pattern.length; i++) {
		c = pattern.charAt(i);
		while ((pattern.charAt(i) == c) && (i < pattern.length)) {
			i++;
		}
		if (c == 'y') {
			year = numbers[arrayPos++];
		} else if (c == 'M') {
			month = numbers[arrayPos++];
		} else if (c == 'd') {
			day = numbers[arrayPos++];
		}
		if (arrayPos > 2) break;
	}
	if (year < 100) {
		if (year < 70) {
			year = year * 1 + 2000;
		} else {
			year = year * 1 + 1900;
		}
	}
	var date = new Date();
	date.setFullYear(year, (month - 1), day);
	return date;
}


Streets.DateTime.padDateFragment = function(value) {
	return (value < 10 ? "0" : "") + value;
}

Streets.DateTime.positionRelativeTo = function(subject, target) {
	var targetPos = YAHOO.util.Dom.getXY(target);
	var targetHeight = YAHOO.util.Dom.get(target).offsetHeight;
	YAHOO.util.Dom.setXY(subject, [targetPos[0],targetPos[1] + targetHeight + 1]);
}

Streets.DateTime.substituteDate = function(pattern, date,oldValue) {
	day = date[2];
	month = date[1];
	year = date[0];
	if(pattern.match(/dd+/)) day = Streets.DateTime.padDateFragment(day);
	if(pattern.match(/MM+/)) month = Streets.DateTime.padDateFragment(month);
	if(!pattern.match(/yy{2}/)) year = Streets.DateTime.padDateFragment(year % 100);
	
	if (!oldValue) oldValue="";
	var numbers = oldValue.match(/(\d+)/g);
	var h,m,s;
	if (numbers != null){
		var arrayPos = 0;
		for (i = 0; i < pattern.length; i++) {
			var c = pattern.charAt(i);
			while ((pattern.charAt(i) == c) && (i < pattern.length)) {
				i++;
			}
			if (c == 'H') {
				h = numbers[arrayPos++];
			} else if (c == 'm') {
				m = numbers[arrayPos++];
			} else if (c == 's') {
				s = numbers[arrayPos++];
			} else {
				arrayPos++;
			}
		}		
	}
	if (!h) h = "00";
	if (!m) m = "00";
	if (!s) s = "00";
	return pattern.replace(/d+/, day).replace(/M+/, month).replace(/y+/, year).replace(/H+/,h).replace(/m+/,m).replace(/s+/,s);
}

Streets.DateTime.showCalendar = function(widget, date, datePattern) {
	if (date) {
		date = Streets.DateTime.parseDate(datePattern, date);
		if (!isNaN(date)) {
			widget.select(date);
			firstDate = widget.getSelectedDates()[0];
			widget.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
			widget.render();
		}
	}
	widget.show();
}

Streets.DateTime.killEvent = function(event){
        if(!event)event=window.event;
        if(!event)return false;
        if(event.cancelBubble!=null){
            event.cancelBubble=true;
        }
        if(event.returnValue){
            event.returnValue=false;
        }
        if(event.stopPropagation){
            event.stopPropagation();
        }
        if(event.preventDefault){
            event.preventDefault();
        }
        return false;
}

Streets.DateTime.init = function(cfg) {
	cfg.dpJs = cfg.widgetId + "DpJs";
	cfg.dp = cfg.widgetId + "Dp";
	cfg.icon = cfg.widgetId +"Icon";
	
	function initIconPosition(){
		var icon = YAHOO.util.Dom.get(cfg.icon);
		icon.style.visibility = "hidden";
		var iconWidth = icon.offsetWidth;
		if (iconWidth<1) iconWidth = 16;
		var obj = YAHOO.util.Dom.get(cfg.widgetId);	
		obj.style.paddingRight = iconWidth;
		icon.style.visibility = "visible";
		var objPos = YAHOO.util.Dom.getXY(obj);
		YAHOO.util.Dom.setXY(icon,[objPos[0] + obj.offsetWidth - iconWidth - 3,objPos[1] + 2]);
	}
	
	var icon = YAHOO.util.Dom.get(cfg.icon);
	icon["initPos"] = initIconPosition;
	var element = YAHOO.util.Dom.get(cfg.widgetId);
    if (element.offsetWidth > 0){
    	initIconPosition();
    } else {
    	window.setTimeout(initIconPosition, 200);
    }
	
    
	YAHOO.namespace("streets");
	
	if (cfg.calendarInit.pages && cfg.calendarInit.pages > 1) {
		YAHOO.streets[cfg.dpJs] = new YAHOO.widget.CalendarGroup(cfg.dpJs,cfg.dp, cfg.calendarInit);
	} else {
		YAHOO.streets[cfg.dpJs] = new YAHOO.widget.Calendar(cfg.dpJs,cfg.dp, cfg.calendarInit);
	}	
	YAHOO.streets[cfg.dpJs].isVisible = function() { return YAHOO.streets[cfg.dpJs].oDomContainer.style.display == 'block'; }
	
	function showCalendar(event) {
		Streets.DateTime.showCalendar(YAHOO.streets[cfg.dpJs], YAHOO.util.Dom.get(cfg.componentId).value, cfg.datePattern);
		if (cfg.alignWithInput) Streets.DateTime.positionRelativeTo(YAHOO.streets[cfg.dpJs].oDomContainer, cfg.widgetId);
		Streets.DateTime.killEvent(event);
	}

	YAHOO.util.Event.addListener(cfg.icon, "click", showCalendar, YAHOO.streets[cfg.dpJs], true);

	function selectHandler(type, args, cal) {
		YAHOO.util.Dom.get(cfg.componentId).value = Streets.DateTime.substituteDate(cfg.datePattern, args[0][0],YAHOO.util.Dom.get(cfg.componentId).value);
		if (cal.isVisible()) {
			if (cfg.hideOnSelect) cal.hide();
			if (cfg.fireChangeEvent) {
				var field = YAHOO.util.Dom.get(cfg.componentId);
				if (field.onchange != null && typeof(field.onchange) != 'undefined') field.onchange();
			}
		}
	}
 
	YAHOO.streets[cfg.dpJs].selectEvent.subscribe(selectHandler,YAHOO.streets[cfg.dpJs]);	 
	YAHOO.streets[cfg.dpJs].render();
}

YAHOO.register("streets-date", Streets.DateTime, {version: "1.0", build: "rc1"});